<?php
/**
 * Created by PhpStorm.
 * User: Jerry
 * Date: 2016/12/21
 * Time: 9:52
 */

namespace SDXApi\Controller;


use SDXApi\Common\Tools\AppVersion;
use SDXApi\Service\HomeAdService;
use SDXApi\Service\HomeTemplateBuyNowService;
use SDXApi\Service\HomeTemplateGoodsNewService;
use SDXApi\Service\HomeTemplateService;
use SDXApi\Service\HomeTemplatesNewService;
use SDXApi\Service\HomeTemplatesService;
use SDXApi\Service\RecommendService;

/**
 * Class HomeTemplateController
 * @package SDXApi\Controller
 */
class HomeTemplateController extends AppController
{
    public function _initialize($apptoken, $apptime)
    {
        parent::_initialize($apptoken, $apptime, false, '',true,false);
    }

    /**
     * Created by Jerry
     */
    public function get_home_template_by_id()
    {
        $id = I('post.template_id', 1, 'int');
        $userId = I('post.uid', 0, 'int');
        $wid = I('post.wid', 8, 'int');

        $is_app =  I('post.is_app', 0, 'int');
        $is_hd = I('post.IS_HD', null, 'int');
        $is_h5 = I('post.is_h5', 0, 'int');
        $oldVersion = true;
        if (!empty($is_app) || !empty($is_h5) || !is_null($is_hd)) {
            $oldVersion = false;
        }

        if (empty($id) || empty($wid)) {
            returnApiError('非法请求');
        }

        $data = array();
        $homeTemplateService = new HomeTemplateService($userId, $wid);
        $homeTemplateService->oldVersion = $oldVersion;

        $templateStencil = $homeTemplateService->getTemplateStencil($id);

        $data['name'] = $templateStencil['name'];
        $data['data'] = $homeTemplateService->getHomeTemplateStencil($id);

        if (empty($data)) {
            returnApiError('暂无数据');
        }

        returnApiSuccess($data);
    }

    /**
     * 返回指定分组数据
     * Created by Jerry
     */
    public function get_home_template_by_group_id()
    {
        $group_id = I('get.group_id', 6, 'int');
        $userId = I('post.uid', 0, 'int');
        $wid = I('post.wid', 8, 'int');
        $isShow = I('post.is_show', 0, 'int');

        if (empty($group_id)) {
            returnApiError('非法请求');
        }

        $homeTemplateService = new HomeTemplatesService($userId, $wid);
        $homeTemplateService->isShow = $isShow;
        $homeTemplateName = $homeTemplateService->getHomeTemplateGroupName($group_id);
        $homeTemplateData = $homeTemplateService->getHomeTemplateByGroup($group_id);

        if (empty($homeTemplateData)) {
            returnApiError('暂无数据');
        }

        $data = array();

        $data['name'] = $homeTemplateName['name'];
        $data['data'] = $homeTemplateData;


        returnApiSuccess($data);
    }

    /**
     * 新版首页接口
     * @author Jerry
     */
    public function get_home_templates()
    {
        $group_id = I('get.group_id', 6, 'int');
        $userId = I('post.uid', 0, 'int');
        $wid = I('post.wid', 8, 'int');
        $isShow = I('post.is_show', 0, 'int');
        $pageNumber = I('post.page', 1, 'int');

        if (empty($group_id)) {
            returnApiError('非法请求');
        }

        $homeTemplateService = new HomeTemplatesNewService($userId, $wid);

        $isNewUser = $homeTemplateService->getIsNewUser($userId);

        $show_bonus_new = 0;
        if ($isNewUser == 1) {
            if ($homeTemplateService->show_bonus_new($userId)){
                $show_bonus_new = 1;
            }
        }

        $key = 'sdx_home_templates_page_'.$group_id.'_'.$wid.'_'.$pageNumber.'_'.$isShow.'_'.$isNewUser.'_'.$show_bonus_new;

        $homeTemplateData = S($key);

        if (empty($homeTemplateData)) {
            $homeTemplateService->isShow = $isShow;
            $homeTemplateData = $homeTemplateService->getHomeTemplateByGroup($group_id, $pageNumber);

            if ($homeTemplateData) {
                S($key, $homeTemplateData, getWriteCacheTime());
            }
        }

        if (empty($homeTemplateData)) {
            returnApiError('暂无数据');
        }

        //处理商品是否订阅、是否已购买过
        //if ($userId == 73248) {
            //$homeTemplateData = $homeTemplateService->handelGoodsBookBuy($homeTemplateData, $userId);
        //}

        returnApiSuccess($homeTemplateData);
    }


    /**
     * Created by Jerry
     * @param $userId
     * @return int
     */
    protected function getUserRank($userId)
    {

        $rank = D('Users')->get_user_rank($userId);

        if (empty($rank)) {
            return -1;
        }

        return $rank['user_rank'];
    }

    /**
     * Created by Jerry
     */
    public function get_buy_now_group_data()
    {
        $userId = I('post.uid', 0, 'int');
        $wid = I('post.wid', 8, 'int');

        $homeTemplateService = new HomeTemplateBuyNowService();
        $groupData = $homeTemplateService->getBuyNowGroupGoods($userId, $wid);

        if (empty($groupData)) {
            returnApiError('数据出错，请返回上一页重进');
        }

        $data = array();
        $data['buy_now_group'] = $groupData;

        returnApiSuccess($data);
    }

    public function testHomeTemplate()
    {
        $group_id = I('post.group_id', 6, 'int');
        $userId = I('post.uid', 0, 'int');
        $wid = I('post.wid', 8, 'int');
        $is_show = I('post.is_show', 0, 'int');

        if (empty($group_id)) {
            returnApiError('非法请求');
        }

        $homeTemplateService = new HomeTemplatesService($userId, $wid);
        $homeTemplateService->isShow = $is_show;
        $homeTemplateName = $homeTemplateService->getHomeTemplateGroupName($group_id);
        $homeTemplateData = $homeTemplateService->getHomeTemplateByGroup($group_id);

        if (empty($homeTemplateData)) {
            returnApiError('暂无数据');
        }


        $data = array();

        $data['name'] = $homeTemplateName['name'];
        $data['data'] = $homeTemplateData;


        returnApiSuccess($data);
    }

    /**
     * 批量清除缓存
     * Created by Jerry
     */
    public function clearCacheHomeTemplate()
    {

        $option = array(
            'del_keys'	=>	true,
        );

        S('home_template_', '', $option);
    }

    /**
     * @author Jerry
     */
    public function getDiscoverGoodsHome()
    {
        $category_id = I('post.cat_id');
        $orientation = I('post.orientation');
        $userId = I('post.uid', 0, 'int');
        $wid = I('post.wid', 8, 'int');

        $discover = new RecommendService();
        $data = $discover->getDiscoverGoodsHome($category_id, $orientation, $userId, $wid);

        if (empty($data)) {
            returnApiError('暂无数据');
        }

        $result['orientation'] = $orientation;
        $result['data'] = $data;
        returnApiSuccess($result);
    }

    /**
     * 获取新品商品数据
     * @author Jerry
     */
    public function getNewGoods()
    {
        $userId = I('post.uid', 0, 'int');
        $wid = I('post.wid', 8, 'int');
        $limit = I('post.goods_limit', 100, 'int');
        $pageNumber = I('post.page', 1, 'int');

        $goodsNewService = new HomeTemplateGoodsNewService();
        $goods = $goodsNewService->getGoodsNewByLimit($userId, $wid, $limit, $pageNumber);


        returnApiSuccess($goods);
    }

    /**
     * @author Jerry
     */
    public function getHomeAd()
    {
        $homeAdService = new HomeAdService();

        $ad = $homeAdService->getHomeAd();

        returnApiSuccess($ad);
    }
}